package mid;

import org.junit.Test;

import java.util.Arrays;

public class a910_最小差值II {
    public int smallestRangeII(int[] A, int K) {
        Arrays.sort(A);
        int len=A.length;
        int result=A[len-1]-A[0];
        for(int i=1;i<len;i++){
            int min=Math.min(A[0]+K,A[i]-K);
            int max=Math.max(A[len-1]-K,A[i-1]+K);
            result=Math.min(max-min,result);
        }
        return result;
    }
    @Test
    public void test(){
        int []arr=new int[]{1,3,6};
        System.out.println(smallestRangeII(arr, 3));

    }
}
